Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table module): add table property setting #71

Merged
merged 1 commit into from
Aug 10, 2024

Conversation

hsuna
Copy link
Contributor

@hsuna hsuna commented Aug 1, 2024

添加表格/单元格属性功能

  1. 设置表格/单元格背景色
  2. 设置表格/单元格边框宽度,颜色,样式
  3. 设置表格/单元格对齐方式

Summary by CodeRabbit

  • New Features

    • Enhanced hover bar functionality, allowing direct modification of table and cell properties.
    • New modal styles improve the appearance and layout of modal components.
    • Added SVG constants for table and cell properties, enhancing UI interactions.
    • New localization strings for improved internationalization support in English and Chinese.
    • Introduced new classes and configurations for managing table and cell properties in the editor.
  • Bug Fixes

    • Corrected functionality related to rendering and parsing table styles.
  • Documentation

    • Updated documentation to reflect new features and localization enhancements.

Copy link
Contributor

coderabbitai bot commented Aug 1, 2024

Walkthrough

The updates introduce enhanced functionalities for managing table properties within a rich text editor. New keys, SVG representations, localization entries, and utility functions are added to improve user interaction and customization of table and cell attributes. These changes collectively aim to enrich the editor's capabilities, providing a more seamless and visually appealing experience for users dealing with tables in their documents.

Changes

Files Change Summary
packages/editor/src/init-default-config/config/... Added setTableProperty and setTableCellProperty to COMMON_HOVERBAR_KEYS.
packages/table-module/src/assets/index.less Enhanced styles for modal components, including new classes for layout and interactivity.
packages/table-module/src/constants/svg.ts Introduced SVG constants for table and cell properties, including alignment options and reset icons.
packages/table-module/src/locale/en.ts Added localization strings for modal properties, colors, and border styles, enriching UI text.
packages/table-module/src/locale/zh-CN.ts Added Chinese translations for modal properties, colors, and border styles.
packages/table-module/src/module/custom-types.ts Defined TableCellProperty type for enhanced styling options, modifying TableCellElement.
packages/table-module/src/module/index.ts Integrated style rendering imports and added new properties for managing table and cell properties.
packages/table-module/src/module/menu/CellProperty.ts Created CellProperty class for managing table cell properties in the editor.
packages/table-module/src/module/menu/TableProperty.ts Developed TableProperty class for managing table properties through a modal interface.
packages/table-module/src/module/menu/index.ts Added setTablePropertyConf and setTableCellPropertyConf for new configurations.
packages/table-module/src/module/parse-style-html.ts Implemented parseStyleHtml to extract styles from HTML tables and cells.
packages/table-module/src/module/render-style.ts Introduced renderStyle function for applying styles to table cells in the virtual DOM.
packages/table-module/src/module/style-to-html.ts Created styleToHtml function to convert node styles into HTML.
packages/table-module/src/utils/dom.ts Added DOM manipulation functions and getOuterHTML utility for accessing full HTML elements.
packages/table-module/src/utils/vdom.ts Introduced addVnodeStyle function for dynamically updating VNode styles.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Editor
    participant Modal
    participant TableProperty
    participant CellProperty

    User->>Editor: Selects table cell
    Editor->>CellProperty: Opens modal
    CellProperty->>Modal: Displays current properties
    User->>Modal: Edits properties
    Modal->>CellProperty: Submits changes
    CellProperty->>Editor: Updates cell attributes
    Editor->>User: Reflects changes in editor
Loading

🐇 In a garden bright, where tables gleam,
New features sprout, like flowers in a dream.
With colors and styles, we hop with glee,
Making edits easy, just wait and see!
So gather 'round, dear friends, take part,
In this joyful update, straight from the heart! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a1c0ea5 and 48d0f3b.

Files selected for processing (15)
  • packages/editor/src/init-default-config/config/hoverbar.ts (1 hunks)
  • packages/table-module/src/assets/index.less (1 hunks)
  • packages/table-module/src/constants/svg.ts (1 hunks)
  • packages/table-module/src/locale/en.ts (2 hunks)
  • packages/table-module/src/locale/zh-CN.ts (2 hunks)
  • packages/table-module/src/module/custom-types.ts (2 hunks)
  • packages/table-module/src/module/index.ts (3 hunks)
  • packages/table-module/src/module/menu/CellProperty.ts (1 hunks)
  • packages/table-module/src/module/menu/TableProperty.ts (1 hunks)
  • packages/table-module/src/module/menu/index.ts (2 hunks)
  • packages/table-module/src/module/parse-style-html.ts (1 hunks)
  • packages/table-module/src/module/render-style.ts (1 hunks)
  • packages/table-module/src/module/style-to-html.ts (1 hunks)
  • packages/table-module/src/utils/dom.ts (3 hunks)
  • packages/table-module/src/utils/vdom.ts (1 hunks)
Additional context used
Biome
packages/table-module/src/module/parse-style-html.ts

[error] 23-23: Unsafe usage of optional chaining.

If it short-circuits with 'undefined' the evaluation will throw TypeError here:

(lint/correctness/noUnsafeOptionalChaining)

Additional comments not posted (43)
packages/table-module/src/module/menu/CellProperty.ts (4)

1-5: LGTM!

The imports and class extension are appropriate.


7-12: LGTM!

The class properties are well-defined and align with the intended functionality.


22-22: LGTM!

The export statement is correct.


14-19: LGTM! But verify the usage of isOfType utility function.

The method getModalContentNode is implemented correctly.

However, ensure that the isOfType utility function is correctly implemented and used.

Verification successful

The isOfType utility function is correctly implemented and used.

The function isOfType is defined in packages/table-module/src/utils/is-of-type.ts and its implementation aligns with its intended purpose. It correctly checks if a node matches the specified types.

  • packages/table-module/src/utils/is-of-type.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation and usage of `isOfType` utility function.

# Test: Search for the implementation of `isOfType`. Expect: Correct implementation and usage.
rg --type js -A 5 $'function isOfType'

Length of output: 39


Script:

#!/bin/bash
# Description: Verify the implementation and usage of `isOfType` utility function.

# Search for the definition of `isOfType` function
ast-grep --lang javascript --pattern 'function isOfType($_, $_) { $$$ }'

# Search for the usage of `isOfType` function
rg 'isOfType'

Length of output: 2776


Script:

#!/bin/bash
# Description: Display the contents of the `is-of-type.ts` file to verify the implementation of `isOfType` function.

# Display the contents of `is-of-type.ts`
cat packages/table-module/src/utils/is-of-type.ts

Length of output: 821

packages/table-module/src/locale/zh-CN.ts (4)

8-14: LGTM!

The translations for modal attributes are correct.


15-18: LGTM!

The translations for color options are correct.


19-29: LGTM!

The translations for border styles are correct.


40-41: LGTM!

The translations for table and cell properties are correct.

packages/table-module/src/module/style-to-html.ts (3)

1-4: LGTM!

The file header is appropriate.


6-7: LGTM!

The imports are appropriate.


8-25: LGTM!

The function implementation is correct and handles the conversion of style properties to HTML appropriately.

packages/table-module/src/utils/vdom.ts (1)

21-32: LGTM! Verify the function usage in the codebase.

The code changes are approved.

However, ensure that the addVnodeStyle function is used correctly throughout the codebase.

Verification successful

The addVnodeStyle function is used correctly throughout the codebase.

The function is utilized in various modules and test files, confirming its proper integration and functionality.

  • packages/table-module/src/module/render-style.ts
  • packages/basic-modules/src/modules/justify/render-style.tsx
  • packages/basic-modules/src/modules/line-height/render-style.tsx
  • packages/basic-modules/src/modules/indent/render-style.tsx
  • packages/basic-modules/src/modules/color/render-style.tsx
  • packages/basic-modules/src/modules/font-size-family/render-style.tsx
  • packages/core/__tests__/utils/vdom.test.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `addVnodeStyle` function in the codebase.

# Test: Search for the function usage. Expect: Correct usage of `addVnodeStyle`.
rg --type js -A 5 $'addVnodeStyle'

Length of output: 33


Script:

#!/bin/bash
# Description: Verify the usage of the `addVnodeStyle` function in the codebase.

# Test: Search for the function usage in both JavaScript and TypeScript files.
rg --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' -tjs -tts -ttsx -A 5 $'addVnodeStyle'

Length of output: 8603

packages/table-module/src/locale/en.ts (4)

8-14: LGTM!

The modal section localization entries are appropriate and consistent.


15-18: LGTM!

The color section localization entries are appropriate and consistent.


19-29: LGTM!

The borderStyle section localization entries are appropriate and consistent.


40-41: LGTM!

The tableProperty and cellProperty localization entries are appropriate and consistent.

packages/table-module/src/module/custom-types.ts (2)

9-16: LGTM!

The TableCellProperty type definition is correct and consistent.


28-28: LGTM!

The TableCellElement type definition is correct and consistent.

packages/table-module/src/module/render-style.ts (1)

1-4: Ensure the utility function addVnodeStyle is correctly implemented.

The function addVnodeStyle is used to add styles to VNodes. Ensure that this utility function correctly handles the style properties.

Verification successful

The addVnodeStyle function is correctly implemented and handles style properties as expected.

The function addVnodeStyle is consistently implemented across multiple files and correctly handles the style properties by ensuring that vnode.data and vnode.data.style are not null before merging the new styles.

  • packages/table-module/src/utils/vdom.ts
  • packages/code-highlight/src/utils/vdom.ts
  • packages/core/src/utils/vdom.ts
  • packages/basic-modules/src/utils/vdom.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of the `addVnodeStyle` function.

# Test: Search for the implementation of `addVnodeStyle`. Expect: Correct handling of style properties.
rg --type js -A 10 $'function addVnodeStyle'

Length of output: 45


Script:

#!/bin/bash
# Description: Verify the implementation of the `addVnodeStyle` function.

# Test: Search for the implementation of `addVnodeStyle` in JavaScript and TypeScript files.
rg --type js --type ts -A 10 'function addVnodeStyle' 
rg --type js --type ts -A 10 'const addVnodeStyle =' 
rg --type js --type ts -A 10 'export function addVnodeStyle'
rg --type js --type ts -A 10 'export const addVnodeStyle ='

Length of output: 4175

packages/editor/src/init-default-config/config/hoverbar.ts (1)

38-40: LGTM! The new keys are correctly integrated.

The new keys setTableProperty and setTableCellProperty are correctly added to the menuKeys array for the table key.

packages/table-module/src/module/index.ts (3)

11-13: LGTM! The new imports are correctly integrated.

The new imports renderStyle, styleToHtml, and parseStyleHtml are correctly added to the file.


26-27: LGTM! The new constants are correctly integrated.

The new constants setTablePropertyConf and setTableCellPropertyConf are correctly added to the menu configuration.


31-33: LGTM! The new properties are correctly integrated.

The new properties renderStyle, styleToHtml, parseStyleHtml, setTablePropertyConf, and setTableCellPropertyConf are correctly added to the table configuration object.

Also applies to: 49-50

packages/table-module/src/assets/index.less (1)

123-196: LGTM! The new styles for modal components are well-structured.

The styles enhance the visual structure and user interaction of modal components.

packages/table-module/src/module/menu/TableProperty.ts (6)

37-40: LGTM! The getValue method is straightforward.

The method returns an empty string as expected.


42-45: LGTM! The isActive method is straightforward.

The method returns false as expected.


47-59: LGTM! The isDisabled method is well-implemented.

The method correctly checks if the selection is within a table and returns the appropriate boolean value.


61-65: LGTM! The exec method is a placeholder.

The method does not contain any logic as expected.


67-72: LGTM! The getModalContentNode method is well-implemented.

The method correctly retrieves the selected table node using the Editor.nodes function.


80-195: LGTM! The getModalContentElem method is well-implemented.

The method correctly constructs the modal content element, initializes form values, and sets up event handlers.

packages/table-module/src/constants/svg.ts (7)

53-54: LGTM! The TABLE_PROPERTY_SVG constant is well-formed.

The SVG is suitable for representing table properties.


57-58: LGTM! The CELL_PROPERTY_SVG constant is well-formed.

The SVG is suitable for representing cell properties.


61-62: LGTM! The JUSTIFY_LEFT_SVG constant is well-formed.

The SVG is suitable for representing left alignment.


65-66: LGTM! The JUSTIFY_RIGHT_SVG constant is well-formed.

The SVG is suitable for representing right alignment.


69-70: LGTM! The JUSTIFY_CENTER_SVG constant is well-formed.

The SVG is suitable for representing center alignment.


73-74: LGTM! The JUSTIFY_JUSTIFY_SVG constant is well-formed.

The SVG is suitable for representing justified alignment.


77-78: LGTM! The CLEAN_SVG constant is well-formed.

The SVG is suitable for representing a clean or reset action.

packages/table-module/src/module/menu/index.ts (2)

91-96: LGTM!

The configuration for setTablePropertyConf is consistent with other configurations.


98-103: LGTM!

The configuration for setTableCellPropertyConf is consistent with other configurations.

packages/table-module/src/utils/dom.ts (4)

38-38: LGTM!

The method $.fn.data is consistent with other methods.


39-39: LGTM!

The method $.fn.hide is consistent with other methods.


40-40: LGTM!

The method $.fn.show is consistent with other methods.


81-84: LGTM!

The function getOuterHTML is consistent with other functions.

Comment on lines +12 to +31
export function renderStyle(node: Descendant, vnode: VNode): VNode {
if (!Element.isElement(node)) return vnode

const { backgroundColor, borderWidth, borderStyle, borderColor, textAlign } =
node as TableCellElement

const props: TableCellProperty = {}
if (backgroundColor) props.backgroundColor = backgroundColor
if (borderWidth) props.borderWidth = `${borderWidth}px`
if (borderStyle) props.borderStyle = borderStyle
if (borderColor) props.borderColor = borderColor
if (textAlign) props.textAlign = textAlign

let styleVnode: VNode = vnode
if (node.type === 'table') {
addVnodeStyle((styleVnode.children?.[0] as VNode).children?.[0] as VNode, props)
} else {
addVnodeStyle(styleVnode, props)
}
return styleVnode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring for readability and performance.

The function can be refactored to improve readability and performance by reducing the number of conditional checks and using a single object to store the properties.

export function renderStyle(node: Descendant, vnode: VNode): VNode {
  if (!Element.isElement(node)) return vnode

  const { backgroundColor, borderWidth, borderStyle, borderColor, textAlign } = node as TableCellElement

  const props: TableCellProperty = {
    ...(backgroundColor && { backgroundColor }),
    ...(borderWidth && { borderWidth: `${borderWidth}px` }),
    ...(borderStyle && { borderStyle }),
    ...(borderColor && { borderColor }),
    ...(textAlign && { textAlign }),
  }

  let styleVnode: VNode = vnode
  if (node.type === 'table') {
    addVnodeStyle((styleVnode.children?.[0] as VNode).children?.[0] as VNode, props)
  } else {
    addVnodeStyle(styleVnode, props)
  }
  return styleVnode
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function renderStyle(node: Descendant, vnode: VNode): VNode {
if (!Element.isElement(node)) return vnode
const { backgroundColor, borderWidth, borderStyle, borderColor, textAlign } =
node as TableCellElement
const props: TableCellProperty = {}
if (backgroundColor) props.backgroundColor = backgroundColor
if (borderWidth) props.borderWidth = `${borderWidth}px`
if (borderStyle) props.borderStyle = borderStyle
if (borderColor) props.borderColor = borderColor
if (textAlign) props.textAlign = textAlign
let styleVnode: VNode = vnode
if (node.type === 'table') {
addVnodeStyle((styleVnode.children?.[0] as VNode).children?.[0] as VNode, props)
} else {
addVnodeStyle(styleVnode, props)
}
return styleVnode
export function renderStyle(node: Descendant, vnode: VNode): VNode {
if (!Element.isElement(node)) return vnode
const { backgroundColor, borderWidth, borderStyle, borderColor, textAlign } = node as TableCellElement
const props: TableCellProperty = {
...(backgroundColor && { backgroundColor }),
...(borderWidth && { borderWidth: `${borderWidth}px` }),
...(borderStyle && { borderStyle }),
...(borderColor && { borderColor }),
...(textAlign && { textAlign }),
}
let styleVnode: VNode = vnode
if (node.type === 'table') {
addVnodeStyle((styleVnode.children?.[0] as VNode).children?.[0] as VNode, props)
} else {
addVnodeStyle(styleVnode, props)
}
return styleVnode
}

}

// eslint-disable-next-line no-unsafe-optional-chaining
let [borderWidth, borderStyle, borderColor] = getStyleValue($elem, 'border')?.split(' ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix unsafe optional chaining usage.

The optional chaining usage here is unsafe and can throw a TypeError. Ensure that getStyleValue($elem, 'border') returns a valid string before using .split(' ').

-  let [borderWidth, borderStyle, borderColor] = getStyleValue($elem, 'border')?.split(' ')
+  const border = getStyleValue($elem, 'border')
+  let [borderWidth, borderStyle, borderColor] = border ? border.split(' ') : [undefined, undefined, undefined]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let [borderWidth, borderStyle, borderColor] = getStyleValue($elem, 'border')?.split(' ')
const border = getStyleValue($elem, 'border')
let [borderWidth, borderStyle, borderColor] = border ? border.split(' ') : [undefined, undefined, undefined]
Tools
Biome

[error] 23-23: Unsafe usage of optional chaining.

If it short-circuits with 'undefined' the evaluation will throw TypeError here:

(lint/correctness/noUnsafeOptionalChaining)

Comment on lines +10 to +42
export function parseStyleHtml(elem: DOMElement, node: Descendant, editor: IDomEditor): Descendant {
if (elem.tagName !== 'TABLE' && elem.tagName !== 'TD') return node

const $elem = $(elem)

let tableNode = node as TableCellElement
let backgroundColor = getStyleValue($elem, 'background-color')
if (!backgroundColor) backgroundColor = getStyleValue($elem, 'background') // word 背景色
if (backgroundColor) {
tableNode.backgroundColor = backgroundColor
}

// eslint-disable-next-line no-unsafe-optional-chaining
let [borderWidth, borderStyle, borderColor] = getStyleValue($elem, 'border')?.split(' ')
borderWidth = getStyleValue($elem, 'border-width') || borderWidth // border 宽度
if (borderWidth) {
tableNode.borderWidth = borderWidth.replace(/[^\d]/g, '')
}
borderStyle = getStyleValue($elem, 'border-style') || borderStyle // border 样式
if (borderStyle) {
tableNode.borderStyle = borderStyle
}
borderColor = getStyleValue($elem, 'border-color') || borderColor // border 颜色
if (borderColor) {
tableNode.borderColor = borderColor
}

let textAlign = getStyleValue($elem, 'text-align')
textAlign = getStyleValue($elem, 'text-align') || textAlign // 文本 对齐
if (textAlign) {
tableNode.textAlign = textAlign
}
return node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize and improve readability.

Consider the following improvements:

  1. Combine the two getStyleValue($elem, 'text-align') calls.
  2. Use const instead of let where variables are not reassigned.
  3. Use a single tableNode variable declaration.
export function parseStyleHtml(elem: DOMElement, node: Descendant, editor: IDomEditor): Descendant {
  if (elem.tagName !== 'TABLE' && elem.tagName !== 'TD') return node

  const $elem = $(elem)
  const tableNode = node as TableCellElement

  let backgroundColor = getStyleValue($elem, 'background-color') || getStyleValue($elem, 'background') // word 背景色
  if (backgroundColor) {
    tableNode.backgroundColor = backgroundColor
  }

  const border = getStyleValue($elem, 'border')
  const [borderWidth, borderStyle, borderColor] = border ? border.split(' ') : [undefined, undefined, undefined]
  const finalBorderWidth = getStyleValue($elem, 'border-width') || borderWidth // border 宽度
  if (finalBorderWidth) {
    tableNode.borderWidth = finalBorderWidth.replace(/[^\d]/g, '')
  }
  const finalBorderStyle = getStyleValue($elem, 'border-style') || borderStyle // border 样式
  if (finalBorderStyle) {
    tableNode.borderStyle = finalBorderStyle
  }
  const finalBorderColor = getStyleValue($elem, 'border-color') || borderColor // border 颜色
  if (finalBorderColor) {
    tableNode.borderColor = finalBorderColor
  }

  const textAlign = getStyleValue($elem, 'text-align')
  if (textAlign) {
    tableNode.textAlign = textAlign
  }

  return node
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function parseStyleHtml(elem: DOMElement, node: Descendant, editor: IDomEditor): Descendant {
if (elem.tagName !== 'TABLE' && elem.tagName !== 'TD') return node
const $elem = $(elem)
let tableNode = node as TableCellElement
let backgroundColor = getStyleValue($elem, 'background-color')
if (!backgroundColor) backgroundColor = getStyleValue($elem, 'background') // word 背景色
if (backgroundColor) {
tableNode.backgroundColor = backgroundColor
}
// eslint-disable-next-line no-unsafe-optional-chaining
let [borderWidth, borderStyle, borderColor] = getStyleValue($elem, 'border')?.split(' ')
borderWidth = getStyleValue($elem, 'border-width') || borderWidth // border 宽度
if (borderWidth) {
tableNode.borderWidth = borderWidth.replace(/[^\d]/g, '')
}
borderStyle = getStyleValue($elem, 'border-style') || borderStyle // border 样式
if (borderStyle) {
tableNode.borderStyle = borderStyle
}
borderColor = getStyleValue($elem, 'border-color') || borderColor // border 颜色
if (borderColor) {
tableNode.borderColor = borderColor
}
let textAlign = getStyleValue($elem, 'text-align')
textAlign = getStyleValue($elem, 'text-align') || textAlign // 文本 对齐
if (textAlign) {
tableNode.textAlign = textAlign
}
return node
export function parseStyleHtml(elem: DOMElement, node: Descendant, editor: IDomEditor): Descendant {
if (elem.tagName !== 'TABLE' && elem.tagName !== 'TD') return node
const $elem = $(elem)
const tableNode = node as TableCellElement
let backgroundColor = getStyleValue($elem, 'background-color') || getStyleValue($elem, 'background') // word 背景色
if (backgroundColor) {
tableNode.backgroundColor = backgroundColor
}
const border = getStyleValue($elem, 'border')
const [borderWidth, borderStyle, borderColor] = border ? border.split(' ') : [undefined, undefined, undefined]
const finalBorderWidth = getStyleValue($elem, 'border-width') || borderWidth // border 宽度
if (finalBorderWidth) {
tableNode.borderWidth = finalBorderWidth.replace(/[^\d]/g, '')
}
const finalBorderStyle = getStyleValue($elem, 'border-style') || borderStyle // border 样式
if (finalBorderStyle) {
tableNode.borderStyle = finalBorderStyle
}
const finalBorderColor = getStyleValue($elem, 'border-color') || borderColor // border 颜色
if (finalBorderColor) {
tableNode.borderColor = finalBorderColor
}
const textAlign = getStyleValue($elem, 'text-align')
if (textAlign) {
tableNode.textAlign = textAlign
}
return node
}
Tools
Biome

[error] 23-23: Unsafe usage of optional chaining.

If it short-circuits with 'undefined' the evaluation will throw TypeError here:

(lint/correctness/noUnsafeOptionalChaining)

@cycleccc
Copy link
Owner

cycleccc commented Aug 3, 2024

我今晚应该会发一版,后面解决了这个 siblings 的问题再发版。

@shitiandmw
Copy link

这个功能啥时候发布呀

@cycleccc
Copy link
Owner

cycleccc commented Aug 7, 2024

这个功能啥时候发布呀

还有点bug,得再看看,需要的话方便看下不,这两天得看下另一个bug

@cycleccc cycleccc merged commit 3c8e314 into cycleccc:master Aug 10, 2024
@hsuna
Copy link
Contributor Author

hsuna commented Aug 12, 2024

我今晚应该会发一版,后面解决了这个 siblings 的问题再发版。

之前没get到,这个是 dom.js 那里漏引入这个fn了~之前赶着上线,所以是独立的文件,合并时漏了这个了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants